<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Manage;
use App\Models\Admins;
use App\Models\Roles;
use Illuminate\Http\Request;

class ManageController extends Controller {

    /**
     * 页面
     * @return view
     */
    public function index(){
        $params = [
            'status' => 1,
        ];
        $roles = Roles::get(['id as value','name as label'])->toJson();
//        dd($roles);
        return view('admin.manage.index',compact('roles'));
    }

    /**
     * 列表数据
     * @param Request $request
     * @return bool
     */
    public function list(Request $request){
        try{
            $params = [
                'kwd'    => (string)$request->get('kwd',''),
                'status' => (int)$request->get('status',0),
            ];
            $lists = Admins::getList($params);
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 更新
     * @param Manage $request
     * @return bool
     */
    public function update(Manage $request)
    {
        try{
            $data = $request->all();
            if($data['id']){
                $info = Admins::findOrFail($data['id']);
                if($data['password']){
                    $data['password'] = bcrypt($data['password']);
                }else{
                    unset($data['password']);
                }
                $info->update($data);
                return $this->writeJson(0,null,'修改管理员信息成功');
            }else{
                unset($data['id']);
                $data['password'] = $data['password'] ? bcrypt($data['password']) : bcrypt('123456');
                Admins::create($data);
                return $this->writeJson(0,null,'新增管理员信息成功');
            }
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 锁定
     * @param Request $request
     * @return bool
     */
    public function lock(Request $request){
        try{
            $id = (int)$request->get('id',0);
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            $info = Admins::findOrFail($id);
            $info->update(['status'=>Admins::STATUS_2]);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 激活
     * @param Request $request
     * @return bool
     */
    public function active(Request $request){
        try{
            $id = (int)$request->get('id',0);
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            $info = Admins::findOrFail($id);
            $info->update(['status'=>Admins::STATUS_1]);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 删除
     * @param Request $request
     * @return bool
     */
    public function destroy(Request $request){
        try{
            $id = (int)$request->get('id',0);
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            Admins::destroy($id);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

}
